namespace Microsoft.Sdc.Tasks.Zip
{
    using System;
    using System.Collections;
    using java.util;

    /// <summary>
    /// Wraps the J# enumeration class
    /// </summary>    
    internal class JSEnumerationWrapper : IEnumerable
    {
        private readonly Enumeration methodEnumeration;

        internal JSEnumerationWrapper(Enumeration method)
        {
            if (method == null)
            {
                throw new ArgumentException();
            }

            this.methodEnumeration = method;
        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return new JSEnumerator(this.methodEnumeration);
        }

        #endregion

        #region Nested type: JSEnumerator

        private class JSEnumerator : IEnumerator
        {
            private readonly Enumeration methodEnumeration;
            private object currentObject;
            private Enumeration wrappedEnumeration;

            internal JSEnumerator(Enumeration method)
            {
                this.methodEnumeration = method;
            }

            #region IEnumerator Members

            public object Current
            {
                get { return this.currentObject; }
            }

            public void Reset()
            {
                this.wrappedEnumeration = this.methodEnumeration;
                if (this.wrappedEnumeration == null)
                {
                    throw new InvalidOperationException();
                }
            }

            public bool MoveNext()
            {
                if (this.wrappedEnumeration == null)
                {
                    this.Reset();
                }

                bool result = this.wrappedEnumeration.hasMoreElements();
                if (result)
                {
                    this.currentObject = this.wrappedEnumeration.nextElement();
                }

                return result;
            }
            #endregion
        }

        #endregion
    }
}